#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This program is free software; you can redistribute it and/or modify  
# it under the terms of the GNU General Public License as published by  
# the Free Software Foundation; either version 2 of the License, or     
# (at your option) any later version.                                   
#                                                                         
# A copy of the license can be found in the license.txt file supplied   
# with this software or at: http://www.gnu.org/copyleft/gpl.html       
#

'''
FORMAT of Database Configuration :

<?xml version="1.0" encoding="utf-8" ?>
<database>
    <commit-log>
        <dir>/var/db/commit</dir>
    </commit-log>
    <metadata>
        <dir>/var/db/metadata</dir>
    </metadata>
</database>
'''

import xml.dom.minidom

from xmlhelper import XmlHelper


CONFIG_FILE_DEFAULT = 'database.xml'

class   Configure(object) :
    configuration = None

    @classmethod
    def instance(cls) :
        if cls.configuration is None :
            cls.load()

        return cls.configuration
        ## instance()

    @classmethod
    def load(cls, config_file=None) :
        if not config_file :
            config_file = CONFIG_FILE_DEFAULT

        config = Configure()
        config.loadConfigure( config_file )
        
        cls.configuration = config
        ## load()

    def __init__(self) :
        self.dir_metadata = None
        self.dir_commit_log = None
        ## __init__()

    def loadConfigure(self, config_file) :
        dom = xml.dom.minidom.parse(config_file)

        self.dir_metadata = XmlHelper.getChildNodeTextByPath(dom, 'database/metadata/dir')
        self.dir_commit_log = XmlHelper.getChildNodeTextByPath(dom, 'database/commit-log/dir')
        ## loadConfigure()

    ## class Configure

